package com.db.service;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import org.apache.commons.io.FileUtils;

import com.base.dowImg;
import com.data.Data;
import com.db.bean.Work;
import com.db.bean.WorkImg;
import com.db.bean.WorkUser;
import com.db.dao.WorkImgDAOImpl;
import com.db.dao.WorkUserDAOImpl;
import com.view.P1;

/**
 * @author 作者:秋哲 E-mail:2810201146@qq.com:
 * @version 创建时间：2020年5月30日 上午7:13:18
 * @description:
 */
public class WorkUserServer {
	private String loadingImg = "src/other/img/loading.png";
	// 全部学生完成情况
	public HashMap<String, DefaultListModel<String>> getClassUserWorkList(int listIndexByGUI) {
		// 获取用户班级ID + 作业ID
		int presidentId = Data.UserBean.getPresident_id();
		Work workObj = Data.workList.get(listIndexByGUI);
		// 作业ID+班级ID 去获取全部的完成情况  写入Data
		List<WorkUser> all = new WorkUserDAOImpl().getAll(workObj.getId(), presidentId);
		Data.workUserMap=new HashMap();
		for (WorkUser workUser : all) {
			Data.workUserMap.put(workUser.getUser_id(), workUser);
		}
//		System.out.println("WorkUserServer");
		return upworkUserAll();
	}
	
	//更新数据
	public HashMap<String, DefaultListModel<String>> upworkUserAll() {
		//生成班级名单 待优化
		Map<Integer,String> roll = new HashMap<>();
		Map<String, Integer> userAll = Data.userAll;
		for(String key:userAll.keySet()) {
			roll.put(userAll.get(key), key);
		}
		HashMap<String, DefaultListModel<String>> map;
		map = new HashMap<String, DefaultListModel<String>>();
		DefaultListModel<String> list02 = new DefaultListModel<>();
		DefaultListModel<String> list03 = new DefaultListModel<>();
		DefaultListModel<String> list04 = new DefaultListModel<>();
		DefaultListModel<String> list05 = new DefaultListModel<>();
		DefaultListModel<String> list06 = new DefaultListModel<>();
		for(Integer key:Data.workUserMap.keySet()) {
			String state = Data.workUserMap.get(key).getState();
			if("None".equals(state)) {
				list02.addElement(roll.get(key));
				roll.remove(key);
			}else if("True".equals(state)) {
				list03.addElement(roll.get(key));
				roll.remove(key);
			}else if("False".equals(state)) {
				list04.addElement(roll.get(key));
				roll.remove(key);
			}else if("Skip".equals(state)) {
				list05.addElement(roll.get(key));
				roll.remove(key);
			}
		}
		for(Integer key:roll.keySet()) {
			String name = roll.get(key);
			list06.addElement(name);
		}
		map.put("list02", list02);
		map.put("list03", list03);
		map.put("list04", list04);
		map.put("list05", list05);
		map.put("list06", list06);
		return map;
	}
	
	private String getMapName(int user_id) {
		Map<String, Integer> userAll = Data.userAll;
		for (String key:userAll.keySet()) {
			Integer integer = userAll.get(key);
			if(integer==user_id)return key;
		}
		return null;
	}
	// 审核作业
	public int auditUserWork(String userNameByGUI,String state) {
		// 得到学生id workID
		Integer user_id = Data.userAll.get(userNameByGUI);
//		System.out.println(user_id);
		WorkUser WorkUser_obj = null;
		Map<Integer, WorkUser> workUserMap = Data.workUserMap;
		for (Integer key : workUserMap.keySet()) {
			WorkUser workUser = workUserMap.get(key);
			if(workUser.getUser_id()==user_id) {
				WorkUser_obj = workUser;
				break;
			}
		}
		WorkUser_obj.setState(state);
		// 修改学生状态
//		System.out.println(WorkUser_obj.toString());
		int n = new WorkUserDAOImpl().updateWorkUser(WorkUser_obj);
		return n;
	}

	// 全部通过
	public int allUserPass(int workID) {
		return workID;

	}

	// 查看作业
	public String getUserWork(String userNameByGUI) {
		Integer index = Data.userAll.get(userNameByGUI);
		WorkUser workUser = Data.workUserMap.get(index);
		int workUser_id = workUser.getId();
		List<WorkImg> all = new WorkImgDAOImpl().getAll(workUser_id);
		String url = null;
		for (WorkImg workImg : all) {
			url = workImg.getUrl();
		}
		// 获取学生 学号+名称
		String username = Data.userHashAll.get(userNameByGUI).getUsername();
		if(url==null) {
			JOptionPane.showMessageDialog(null, "图片地址为空");
			return loadingImg;
			
		};
		String[] splitList = url.split("\\.");
		String fileType = splitList[splitList.length-1];
		String  fileName = username + " " + userNameByGUI + "." +fileType;
		// 获取 选中的组名称和作业名称
		Object selectedItem = P1.instance.com01.getSelectedItem();
		String selectedValue = P1.instance.list01.getSelectedValue();
		return this.saveWork("data/"+selectedItem+"/"+selectedValue,fileName,url);
	}
	/**
	 * 
	 * @version 创建时间：2020年6月21日 下午2:20:21
	 * @description   保存作业文件到本地
	 * @param fileUrl
	 * @param fileName
	 * @param interntUrl
	 * @return
	 */
	public String saveWork(String fileUrl,String fileName,String interntUrl) {
		
		File file = new File(fileUrl,fileName);
		//判断是否为新文件
			//对象转文件 
			//保存旧的文件名称  缓存  读取时机  更新时机
			//拿到旧的文件名称  //拿到新的文件名称
		//判断本地是否存在  调用本地  直接下载
		
		if(file.exists()) {
			file.delete();
//			return file.getPath(); //返回本地地址
		}else {
			
		}
		Data.imgUrl = file.getPath();
		new dowImg(interntUrl, file).start();
		return loadingImg; //返回加载中的图片
	}
}


